package org.train.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.train.entity.ReportPo;
import org.train.util.R;
import org.train.entity.RevenueExpenditurePo;
import org.train.service.RevenueExpenditureService;
import org.train.util.RUtil;
import org.train.util.Renum;

import java.util.List;

@RestController
@RequestMapping("/revenueExpenditure")
public class RevenueExpenditureController{
    @Autowired
    private RevenueExpenditureService service;

    @GetMapping("/queryAllDetailsOfOneProject")
    public R queryAllDetailsByProjectId(@RequestParam Integer id){
        return RUtil.success(service.list(new QueryWrapper<RevenueExpenditurePo>()
                .eq("project_id", id)
                .eq("deleted", 0))
        );
    }

    @GetMapping("/queryAllInfos")
    public R queryAllInfos(Integer pageNum, Integer pageSize, String search){
        return service.queryAllInfos(pageNum, pageSize, search);
    }

    @PostMapping("/saveOrUpdateOne")
    public R saveOrUpdate(@RequestBody RevenueExpenditurePo po){
        if(po.getProjectId() == null
            || po.getGenerateTime() == null
            || po.getAmount() == null
            || po.getType() == null){
            return RUtil.error(Renum.REQUIRED_FIELDS_MISSING);
        }

        return service.saveOrUpdateOne(po);
    }

    @PostMapping("/removeBatch")
    public R removeBatch(@RequestBody List<Integer> ids){
        service.update(new UpdateWrapper<RevenueExpenditurePo>()
                .in("id", ids)
                .set("deleted", 1));
        return RUtil.success();
    }
}
